package View.Venda;

import Model.VendaModel;
import Repositories.VendaRepository;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.netbeans.lib.awtextra.AbsoluteLayout;
/**
 *
 * @author Yanne
 */
public class Venda extends javax.swing.JDialog {

    DefaultTableModel model = new DefaultTableModel(new Object[][]{}, new Object[]{"Código", "Mesa", "Valor"}){
            public boolean isCellEditable(int rowIndex, int mColIndex) {  
                return false;  
            }  
        };  
    VendaRepository vendaRepository = new VendaRepository();

    /** Creates new form Venda */
    public Venda(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        initComponents();

        AbsoluteLayout absoluto = null;
        jP_vendas.setLayout(absoluto);

        //bg novo usuario?
        JLabel lst_prdt = new JLabel(new ImageIcon("./img/bg_vendas.png"));
        jP_vendas.add(lst_prdt);
        //jP_vendas.setVisible(true);
        lst_prdt.setBounds(0, 0, 580, 480);

        TabelaVendas.setModel(model);

        CarregarVendas();
        this.setLocationRelativeTo(null);
        // this.setResizable(false);
    }

    private void CarregarVendas() {
        try {
            ArrayList<VendaModel> vendas = vendaRepository.ListadeVendas();
            if (vendas != null) {
                DefaultTableModel modelo = new DefaultTableModel(new Object[][]{}, new Object[]{"Código", "Mesa", "Valor"}){
            public boolean isCellEditable(int rowIndex, int mColIndex) {  
                return false;  
            }  
        };  
                for (VendaModel venda : vendas) {
                    modelo.addRow(new Object[]{venda.getVENDA_ID(), venda.getVENDA_MESA(), venda.getVENDA_VALOR()});
                }

                TabelaVendas.setModel(modelo);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jP_vendas = new javax.swing.JPanel();
        btnFazerPedido = new javax.swing.JButton();
        btnAlterarPedido = new javax.swing.JButton();
        btnPedidoAtendido = new javax.swing.JButton();
        btnSair = new javax.swing.JToggleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TabelaVendas = new javax.swing.JTable();
        btnCancelarPedido = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jP_vendas.setBackground(new java.awt.Color(255, 153, 255));
        jP_vendas.setMinimumSize(new java.awt.Dimension(580, 480));
        jP_vendas.setPreferredSize(new java.awt.Dimension(580, 480));
        jP_vendas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnFazerPedido.setText("Fazer Pedido");
        btnFazerPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFazerPedidoActionPerformed(evt);
            }
        });
        jP_vendas.add(btnFazerPedido, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 50, 130, 59));

        btnAlterarPedido.setText("Alterar Pedido");
        btnAlterarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarPedidoActionPerformed(evt);
            }
        });
        jP_vendas.add(btnAlterarPedido, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 120, 130, 59));

        btnPedidoAtendido.setText("Pedido Atendido");
        btnPedidoAtendido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPedidoAtendidoActionPerformed(evt);
            }
        });
        jP_vendas.add(btnPedidoAtendido, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 260, 130, 59));

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jP_vendas.add(btnSair, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 330, 130, 57));

        TabelaVendas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(TabelaVendas);

        jP_vendas.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 380, 360));

        btnCancelarPedido.setText("Cancelar Pedido");
        btnCancelarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarPedidoActionPerformed(evt);
            }
        });
        jP_vendas.add(btnCancelarPedido, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 190, 130, 59));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jP_vendas, javax.swing.GroupLayout.PREFERRED_SIZE, 580, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jP_vendas, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
    this.dispose();
}//GEN-LAST:event_btnSairActionPerformed

private void btnAlterarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarPedidoActionPerformed
    try {
        Object objeto = TabelaVendas.getValueAt(TabelaVendas.getSelectedRow(), 0);
        int IdVenda = Integer.parseInt(objeto.toString());

        objeto = TabelaVendas.getValueAt(TabelaVendas.getSelectedRow(), 1);
        int Mesa = Integer.parseInt(objeto.toString());

        Caixa caixa = new Caixa(this, true, IdVenda, Mesa);
        caixa.show(true);
    } catch (ArrayIndexOutOfBoundsException ex) {
        JOptionPane.showMessageDialog(null, "É necessário selecionar um pedido!");
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
}//GEN-LAST:event_btnAlterarPedidoActionPerformed

private void btnPedidoAtendidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPedidoAtendidoActionPerformed
    try {
        Object objeto = TabelaVendas.getValueAt(TabelaVendas.getSelectedRow(), 0);
        int IdVenda = Integer.parseInt(objeto.toString());
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "Confirma o atendimento desse pedido?")) {


            if (vendaRepository.EfetuarVenda(IdVenda)) {
                JOptionPane.showMessageDialog(null, "Venda Efetuada!");
                CarregarVendas();

            }
        }
    } catch (ArrayIndexOutOfBoundsException ex) {
        JOptionPane.showMessageDialog(null, "É necessário selecionar um pedido!");
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
}//GEN-LAST:event_btnPedidoAtendidoActionPerformed

private void btnFazerPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFazerPedidoActionPerformed
    Caixa caixa = new Caixa(this, true);
    caixa.show();
    CarregarVendas();
}//GEN-LAST:event_btnFazerPedidoActionPerformed

private void btnCancelarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarPedidoActionPerformed
    try {
        Object objeto = TabelaVendas.getValueAt(TabelaVendas.getSelectedRow(), 0);
        int IdVenda = Integer.parseInt(objeto.toString());
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "Confirma o cancelamento desse pedido?")) {


            if (vendaRepository.Deletar(IdVenda)) {
                JOptionPane.showMessageDialog(null, "Venda Cancelada!");
                CarregarVendas();

            }
        }
    } catch (ArrayIndexOutOfBoundsException ex) {
        JOptionPane.showMessageDialog(null, "É necessário selecionar um pedido!");
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
}//GEN-LAST:event_btnCancelarPedidoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Venda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Venda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Venda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Venda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Venda dialog = new Venda(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TabelaVendas;
    private javax.swing.JButton btnAlterarPedido;
    private javax.swing.JButton btnCancelarPedido;
    private javax.swing.JButton btnFazerPedido;
    private javax.swing.JButton btnPedidoAtendido;
    private javax.swing.JToggleButton btnSair;
    private javax.swing.JPanel jP_vendas;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
